<template>
  <!-- 选择实验室检查 -->
  <section>
    <el-drawer size="900px" title="选择实验室检查" :visible="value" custom-class="drawer-box" :wrapperClosable="false" :destroy-on-close="true" @open="onOpen" @close="onClose" :append-to-body="true">
      <div class="lab-box">
        <div>
          <el-form :model="form" :inline="true" @submit.native.prevent="onSearch">
            <el-form-item>
              <el-input v-model="form.queryStr" placeholder="检查名称、简码"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" native-type="submit" circle icon="el-icon-search"></el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="form-tool">
          <div class="tree-box">
            <el-tree ref="tree" :data="treeList" :props="treeProp" highlight-current node-key="id" @node-click="handleNodeClick"></el-tree>
          </div>
          <div>
            <div v-for="(item,index) in tableList" class="check-box">
              <el-checkbox v-model="item.check" @change="onChangeCache($event,item)" :key="item.id">{{item.name}}</el-checkbox>
            </div>
            <div v-if="tableList.length == 0" style="color:#999;padding:10px 0;text-align:center;">
              <i class="iconfont iconwushuju" style="font-size:40px;"></i>
              <div>暂无数据</div>
            </div>
            <br />
            <pager v-model="pager" small @change-pagenum="getData"></pager>
          </div>
        </div>
        <div>
          <el-button @click="onClose">取消</el-button>
          <el-button type="primary" @click="onSubmit">保存</el-button>
        </div>
      </div>
    </el-drawer>
  </section>
</template>
<script>
export default {
  props: ["value"],
  data() {
    return {
      treeList: [],
      pager: { pageSize: 20, pageNum: 1, total: 0 },
      tableList: [],
      treeProp: {
        label: "name",
        children: "child"
      },
      form: {
        queryStr: "",
        typeId: ""
      },
      curList: []
    };
  },
  methods: {
    onChangeCache(event, obj) {
      const th = this;
      let index = th.curList.findIndex(d => {
        return d.id == obj.id;
      });
      if (event) {
        if (index == -1) th.curList.push(obj);
      } else {
        if (index != -1) th.curList.splice(index, 1);
      }
    },
    //提交
    onSubmit() {
      const th = this;
      if (th.curList.length == 0) {
        th.$message.warning("请选择检查");
        return;
      }
      th.$emit("submit", th.curList);
      th.onClose();
    },
    //点击树
    handleNodeClick(data, Node, self) {
      const th = this;
      th.form.typeId = data.id;
      th.getData(-1);
    },
    getTrue() {
      const th = this;
      th.$api.medicalcheckuptypetree({}).then(res => {
        if (res.executed) {
          res.data.unshift({
            id: -1,
            name: "全部",
            parentId: -1
          });
          th.treeList = res.data;
        }
      });
    },
    onSearch() {
      const th = this;
      th.form.typeId = "";
      th.$refs["tree"].setCurrentKey(-1);
      th.getData(-1);
    },
    getData(type) {
      const th = this;
      if (type == -1) {
        th.pager.pageNum = 1;
      }
      let j = {};
      Object.assign(j, th.pager);
      if (th.form.typeId) {
        j.typeId = th.form.typeId;
      } else {
        j.queryStr = th.form.queryStr;
      }
      th.$api.medicalcheckuppageQuery(j).then(res => {
        if (res.executed) {
          th.tableList = res.dataBody.list.map(d => {
            d.check=false;
            // let index = th.curList.findIndex(ele => {
            //   return d.id == ele.id;
            // });
            // if (index != -1) d.check = true;
            return d;
          });
          th.pager.total = res.dataBody.total;
        }
      });
    },
    onOpen() {
      const th = this;
      th.getTrue();
      th.getData(-1);
    },
    onClose() {
      const th = this;
      th.curList = [];
      th.$emit("input", false);
    }
  }
};
</script>
<style lang="scss" scoped>
.drawer-box {
  .lab-box {
    display: flex;
    height: 100%;
    flex-direction: column;
    > div:nth-child(1) {
      flex-shrink: 0;
      padding: 10px 10px 0 10px;
      border-bottom: 1px solid #eee;
    }
    > div:nth-child(2) {
      flex-grow: 1;
      height: 100%;
      overflow-y: auto;
      > div:nth-child(1) {
        overflow: auto;
        height: 100%;
      }
      > div:nth-child(2) {
        width: 500px;
        overflow: auto;
        height: 100%;
        border-left: 1px solid #eee;
        padding: 0;
      }
    }
    > div:nth-child(3) {
      flex-shrink: 0;
      border-top: 1px solid #eee;
      padding: 10px;
      text-align: right;
    }
  }
  .check-box {
    padding: 7px 10px;
    &:hover {
      background-color: #eee;
      cursor: pointer;
    }
  }
}
</style>